package com.sctu.httpvollry;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;

import com.android.volley.*;
import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

/**
 *获取网络数据
 * 
 */
public class GetVolleyHttp {

	private static GetVolleyHttp getjsonvollery = null;
	private Activity activity;
	private RequestQueue request;

	/**
	 * 构造函数
	 * 
	 * @param activity
	 */
	private GetVolleyHttp(Activity activity) {
		this.activity = activity;
		request = Volley.newRequestQueue(activity);
	}

	/**
	 * 获取实例 单列模式
	 * 
	 * @param activity
	 *            当前activity
	 * @return
	 */
	public static GetVolleyHttp getInstance(Activity activity) {
		if (getjsonvollery == null) {
			getjsonvollery = new GetVolleyHttp(activity);
		}
		return getjsonvollery;
	}

	/**
	 * 获取StringJson数据 Get方式
	 * 
	 * @param url
	 *            url地址
	 * @param listener
	 *            成功接口
	 * @param errorlistener
	 *            失败接口
	 */
	public void getStringGETJsonData(final String url, Listener<String> listener, ErrorListener errorlistener) {
        if(request.getCache().get(url)!=null){
            Long startTime = request.getCache().get(url).serverDate;
            Long nowTime = new Date().getTime();
            if ((nowTime - startTime)> 4*60*60*1000){                                 //每4小时更新一次数据
                StringRequest stringrequest = new StringRequest(Method.GET, url, listener, errorlistener);
                stringrequest.setShouldCache(Boolean.TRUE);
                request.add(stringrequest);
            }else {
                listener.onResponse(new String(request.getCache().get(url).data));
            }
        }else {
            StringRequest stringrequest = new StringRequest(Method.GET, url, listener, errorlistener);
            stringrequest.setShouldCache(Boolean.TRUE);
            request.add(stringrequest);
        }



	}

    public void getStringGETJsonData(final String url, Listener<String> listener, ErrorListener errorlistener,Long time) {
        if(request.getCache().get(url)!=null){
            Long startTime = request.getCache().get(url).serverDate;
            Long nowTime = new Date().getTime();
            if ((nowTime - startTime)> time){                                 //每4小时更新一次数据
                StringRequest stringrequest = new StringRequest(Method.GET, url, listener, errorlistener);
                stringrequest.setShouldCache(Boolean.TRUE);
                request.add(stringrequest);
            }else {
                listener.onResponse(new String(request.getCache().get(url).data));
            }
        }else {
            StringRequest stringrequest = new StringRequest(Method.GET, url, listener, errorlistener);
            stringrequest.setShouldCache(Boolean.TRUE);
            request.add(stringrequest);
        }



	}


	/**
	 * 获取StringJson数据 POST方式
	 * 
	 * @param url
	 *            url地址
	 * @param listener
	 *            成功接口
	 * @param errorlistener
	 *            失败接口
	 * @param map
	 *            参数
	 */
	public void getStringPOSTJsonData(final String url,final HashMap<String, String> map , Listener<String> listener, ErrorListener errorlistener) {
		StringRequest stringrequest = new StringRequest(Method.POST, url, listener, errorlistener) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				return map;
			}
		};
		stringrequest.setShouldCache(Boolean.TRUE);
		request.add(stringrequest);
	}

	/**
	 * 网络图片获取
	 * 
	 * @param imageview
	 *            图片控件
	 * @param defaultImageResId
	 *            默认图片
	 * @param errorImageResId
	 *            加载错误图片
	 * @param url
	 *            url图片地址
	 */
	@SuppressWarnings("static-access")
	public void setImageLoader(ImageView imageview, int defaultImageResId, int errorImageResId, String url) {
        if(request.getCache().get(url)!=null){
            Long startTime = request.getCache().get(url).serverDate;
            Long nowTime = new Date().getTime();
            if ((nowTime - startTime)/1000 > 4*60*60*1000){                          //每4小时更新一次数据
                ImageLoader imageloder = RequestManager.getInstance(request, activity).getImageLoader();
                imageloder.get(url, imageloder.getImageListener(imageview, defaultImageResId, errorImageResId));
            }else {
                Bitmap bitmap = BitmapFactory.decodeByteArray(request.getCache().get(url).data, 0, request.getCache().get(url).data.length);
                imageview.setImageBitmap(bitmap);
            }

        }else {
            ImageLoader imageloder = RequestManager.getInstance(request, activity).getImageLoader();
            imageloder.get(url, imageloder.getImageListener(imageview, defaultImageResId, errorImageResId));
        }


	}

    @SuppressWarnings("static-access")
	public void setImageLoader(ImageView imageview, int defaultImageResId, int errorImageResId, String url,Long refreshTime) {
		ImageLoader imageloder = RequestManager.getInstance(request, activity).getImageLoader();
        if(request.getCache().get(url)!=null){
            //response exists
            Long startTime = request.getCache().get(url).serverDate;
            Long nowTime = new Date().getTime();
            if (nowTime - startTime > refreshTime){                      //用户自定义时间更新一次数据
                try {
                    imageloder.get(url, new ImageLoader.ImageListener() {
                        @Override
                        public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
                            imageContainer.getBitmap();
                        }

                        @Override
                        public void onErrorResponse(VolleyError volleyError) {

                        }
                    });
                }catch (Exception e){
                    String cachedResponse = new String(request.getCache().get(url).data);
                    Bitmap bitmap = BitmapFactory.decodeByteArray(request.getCache().get(url).data, 0, request.getCache().get(url).data.length);
                    imageview.setImageBitmap(bitmap);
                }

            }else {
                Bitmap bitmap = BitmapFactory.decodeByteArray(request.getCache().get(url).data, 0, request.getCache().get(url).data.length);
                imageview.setImageBitmap(bitmap);
            }
        }else{

            imageloder.get(url, imageloder.getImageListener(imageview, defaultImageResId, errorImageResId));
        }

	}


}
